﻿using Windows.Storage;

namespace DimensionsHelper.Client.WinUi.Helpers;

internal class StorageHelper
{
    public static string GetUriOrPathText(string relativePath)
    {
        if (RuntimeHelper.IsMSIX)
        {
            return "ms-appx:///" + relativePath;
        }

        return Path.GetFullPath(Path.Combine(AppContext.BaseDirectory, relativePath));
    }


    public static async Task<StorageFile> LoadFile(string relativePath)
    {
        StorageFile file;
        string pathOrUri = GetUriOrPathText(relativePath);

        if (RuntimeHelper.IsMSIX)
        {
            Uri sourceUri = new(pathOrUri);
            file = await StorageFile.GetFileFromApplicationUriAsync(sourceUri);
        }
        else
        {
            file = await StorageFile.GetFileFromPathAsync(pathOrUri);
        }

        return file;
    }


    public static async Task<string> LoadText(string relativePath)
    {
        StorageFile file = await LoadFile(relativePath);
        return await FileIO.ReadTextAsync(file);
    }
}